package cerebrum.controller;

import cerebrum.common.EmptyChecker;
import cerebrum.enums.LogCode;
import cerebrum.enums.SetUpMeaages;
import cerebrum.listener.DownloadFinishListener;
import cerebrum.listener.RequestFinishListener;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

public class ImagesDownload {


    /**
     * 检查当天的下载是否已经执行，防止重复下载
     *
     * @return
     */
    private static boolean check() {

        LocalDate date = LocalDate.now();
        String name = String.valueOf(date);
        File file1 = new File(SetUpMeaages.logPath.getPath() + name + ".txt");
        if (file1.exists()) {
            return true;
        }
        return false;
    }

    public static void downWallPaper1(String requestAddress) throws IOException {
        if (!check()) {
            downPhoto(requestAddress);
        }
    }

    public static void downWallPaper() {
        if (!check()) {
//            String requestAddress = "http://guolin.tech/api/bing_pic";
            String requestAddress = "https://img.catct.cn";
            HttpUtil.doGet(requestAddress, new RequestFinishListener() {

                @Override
                public void finish(String response) {
                    downPhoto(response);
                }

                @Override
                public void error(Exception ex) {
                    LogUtils.saveLog(LogCode.FAILED.getValue(), "请求图片网址时异常: " + ex.toString(), null);
                }
            });
        }
    }

    // 下载图片
    public static void downPhoto(String picAddress) {
        LocalDate now = LocalDate.now();
        //获取图片保存地址
        String dir = SetUpMeaages.imagesPath.getPath() + "\\" + now;
        File saveDir = new File(dir);
        //目录是否存在
        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }

        //获取全部文件
        File[] allFiles = saveDir.listFiles();
        if (EmptyChecker.isEmpty(allFiles)) {
            // 文件夹不存在图片，从1开始命名
            String name = "1.jpg";
            HttpUtil.download(picAddress, dir, name, new DownloadFinishListener() {

                @Override
                public void finish(String url) {
                    LogUtils.saveLog(LogCode.SUCCEED.getValue(), "下载成功", url);
                }

                @Override
                public void error(Exception ex, String url) {
                    LogUtils.saveLog(LogCode.FAILED.getValue(), "下载图片时异常： " + ex.toString(), url);
                }
            });
        } else {
            int names[] = new int[allFiles.length];
            for (int i = 0; i < allFiles.length; i++) {
                String fileName = allFiles[i].getName();
                names[i] = Integer.parseInt(fileName.substring(0, fileName.lastIndexOf(".")));
            }
            String name = LogUtils.getMax(names) + 1 + ".jpg";
            HttpUtil.download(picAddress, dir, name, new DownloadFinishListener() {

                @Override
                public void finish(String url) {
                    LogUtils.saveLog(LogCode.SUCCEED.getValue(), "下载成功", url);
                }

                @Override
                public void error(Exception ex, String url) {
                    LogUtils.saveLog(LogCode.FAILED.getValue(), "下载图片时异常： " + ex.toString(), url);
                }
            });
        }
    }

}
